#include "mypushbutton.h"
#include<QDebug>
#include<QPropertyAnimation>
// MyPushButton::MyPushButton(QWidget *parent)
//     : QPushButton{parent}
// {}

//按钮图片的加载
MyPushButton::MyPushButton(QString normalImag,QString pressImag)
{
    this->normalImagPath=normalImag;
    this->pressImagPath=pressImag;
    QPixmap pix;
    bool ret=pix.load(normalImag);
    if(!ret)
    {
        qDebug()<<"图片加载失败";
        return;
    }
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));
}
//弹跳动画
void MyPushButton:: zoom1()//向下
{
    QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");
    animation->setDuration(200);
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}
void MyPushButton:: zoom2()//向上
{
    QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");
    animation->setDuration(200);
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}
//鼠标按下
void MyPushButton:: mousePressEvent(QMouseEvent *e)
{
    if(this->pressImagPath!="")
    {
        QPixmap pix;
        bool ret=pix.load(this->pressImagPath);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
            return;
        }
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mousePressEvent(e);
}
//鼠标释放
void MyPushButton:: mouseReleaseEvent(QMouseEvent *e)
{
    if(this->pressImagPath!="")
    {
        QPixmap pix;
        bool ret=pix.load(this->normalImagPath);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
            return;
        }
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mouseReleaseEvent(e);
}
